package 链表.合并两个有序列表;

/**
 * @Program:IntelliJ IDEA
 * @description:
 * @author:香蕉你个啵娜娜 l.h-p@qq.com
 * @create:2023-01-05 20:50
 **/

public class Solution {
    public ListNode mergeTwoLists(ListNode linked1, ListNode linked2) {
        // 进行空判断
        if (linked1 == null) {
            return linked2;
        }
        if (linked2 == null){
            return linked1;
        }
        ListNode dummy = new ListNode(0);
        ListNode curr = dummy;
        while (linked1 != null && linked2 != null){
            // 比较一下，哪个小就把哪个放到新的链表
            if (linked1.val <= linked2.val) {
                curr.next = linked1;
                linked1 = linked1.next;
            } else {
                curr.next = linked2;
                linked2 = linked2.next;
            }
            curr = curr.next;
        }
        // 然后把那个不为空的链表挂到新链表中
        curr.next = linked1 == null ? linked2 : linked1;
        return dummy.next;
    }
}
